import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'core/routes.dart';
import 'core/themes.dart';
import 'core/constants.dart';
import 'presentation/viewmodels/game_viewmodel.dart';
import 'data/repositories/image_repository.dart';

void main() async {
  // 确保Flutter绑定初始化
  WidgetsFlutterBinding.ensureInitialized();
  
  // 预初始化关键服务
  await _initializeServices();
  
  runApp(const MyApp());
}

/// 预初始化服务
Future<void> _initializeServices() async {
  try {
    debugPrint('🚀 [Main] 开始初始化服务...');
    
    // 初始化图片仓库
    final imageRepository = ImageRepository();
    await imageRepository.initialize();
    
    debugPrint('✅ [Main] 服务初始化完成');
  } catch (e) {
    debugPrint('❌ [Main] 服务初始化失败: $e');
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        // 游戏状态管理
        ChangeNotifierProvider(create: (_) => GameViewModel()),
        
        // 其他Provider将在V2.0添加
        // ChangeNotifierProvider(create: (_) => SettingsViewModel()),
        // ChangeNotifierProvider(create: (_) => LeaderboardViewModel()),
      ],
      child: MaterialApp(
        title: AppConstants.appName,
        debugShowCheckedModeBanner: false,
        
        // 主题配置
        theme: AppThemes.lightTheme,
        darkTheme: AppThemes.darkTheme,
        themeMode: ThemeMode.system,
        
        // 路由配置
        initialRoute: Routes.home,
        routes: Routes.routes,
        onGenerateRoute: Routes.onGenerateRoute,
        
        // 性能优化
        builder: (context, child) {
          return MediaQuery(
            data: MediaQuery.of(context).copyWith(
              textScaler: const TextScaler.linear(1.0), // 防止系统字体缩放影响布局
            ),
            child: child!,
          );
        },
      ),
    );
  }
}
